//
//  MJMyInfoViewModel.m
//  MJ
//
//  Created by qiaoBu on 2017/12/11.
//  Copyright © 2017年 lina. All rights reserved.
//

#import "MJMyInfoViewModel.h"
#import "MJNetWorkTool.h"
#import "MJMyHistoryExamsModel.h"

@implementation MJMyInfoViewModel

+ (void)requestMyInfoSuccess:(void (^)(NSString *stateCode,MJMeInfoModel *infoModel,NSString *messageState))requestSuccess failure:(void(^)(NSError *error))requestFailure netWorkState:(void (^) (BOOL netWorkState))netWorkState{
    
    [[MJNetWorkTool sharedUtil] requestAFURL:MJ_UserInformation httpMethod:1 parameters:[NSMutableDictionary dictionary] succeed:^(id dict) {
        
        NSDictionary *resultDict = [NSDictionary dictionaryWithDictionary:dict];
        NSString *stateCode = [resultDict objectForKey:@"flag"];
        
        if ([stateCode isEqualToString:@"001"])
        {
            MJMeInfoModel *model = [MJMeInfoModel modelWithDictionary:[resultDict objectForKey:@"content"]];
            requestSuccess (stateCode,model,nil);
        }
        else
        {
            if ([stateCode isEqualToString:@"203"])
            {
                    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:IsLogined];
            }
            requestSuccess (stateCode,nil,[resultDict objectForKey:@"content"]);
        }
        
    } failure:^(NSError *err) {
        requestFailure (err);
    }];
}
+ (void)requestUploadHeadImageWithImagePath:(NSString *)filePath andNSMutableDictionary:(NSMutableDictionary *)initializedDict Success:(void (^) (NSString *stateCode,NSString *imageUrl,NSString *messageState))requestSuccess failure:(void(^)(NSError *error))requestFailure netWorkState:(void (^) (BOOL netWorkState))netWorkState{
    
    [[MJNetWorkTool sharedUtil] requestAFURL:MJ_UploadImage parameters:initializedDict fileName:filePath succeed:^(id dict) {
        
        NSDictionary *resultDict = [NSDictionary dictionaryWithDictionary:dict];
        NSString *stateCode = [resultDict objectForKey:@"flag"];
        
        if ([stateCode isEqualToString:@"001"])
        {   //上传封面成功，
            NSDictionary *urlContent = [NSDictionary dictionaryWithDictionary:[resultDict objectForKey:@"content"]];
            NSString *imageUrl =  [[urlContent objectForKey:@"b"]  objectForKey:@"link"];
            
            NSLog(@"urlContent --------%@---%@",urlContent,imageUrl);
            requestSuccess (stateCode,imageUrl,nil);
        }
        else
        {
            NSLog(@"errorContent-------%@",[resultDict objectForKey:@"content"]);
            requestSuccess (stateCode,nil,[resultDict objectForKey:@"content"]);
        }
    } failure:^(NSError *error){
        
        requestFailure (error);
        NSLog(@"error --- %@",error);
    }];
}
+ (void)requestChangeInfoWithDictionary:(NSMutableDictionary *)initializedDict Success:(void (^) (NSString *stateCode,NSString *messageState))requestSuccess failure:(void(^)(NSError *error))requestFailure netWorkState:(void (^) (BOOL netWorkState))netWorkState{
    
    [[MJNetWorkTool sharedUtil] requestAFURL:MJ_Me_ModifyPersonalInfo httpMethod:1 parameters:initializedDict succeed:^(id dict) {
        
        NSDictionary *resultDict = [NSDictionary dictionaryWithDictionary:dict];
        NSString *stateCode = [resultDict objectForKey:@"flag"];
        
        if ([stateCode isEqualToString:@"001"])
        {
             requestSuccess (stateCode,nil);
        }
        else
        {
            if ([stateCode isEqualToString:@"203"])
            {
                [[NSUserDefaults standardUserDefaults] setBool:NO forKey:IsLogined];
            }
            requestSuccess (stateCode,[resultDict objectForKey:@"content"]);
        }
        
    } failure:^(NSError *err) {
        requestFailure (err);
    }];
    
}
+ (void)requestMyHistoryExamsWithKey:(NSString *)key Success:(void (^) (NSString *stateCode,MJMyHistoryExamsModel *model,NSString *messageState))requestSuccess failure:(void(^)(NSError *error))requestFailure netWorkState:(void (^) (BOOL netWorkState))netWorkState{
    
    NSMutableDictionary *para = [NSMutableDictionary dictionary];
    [para setObject:[NSString stringWithFormat:@"%@",key] forKey:@"record"];
    
    [[MJNetWorkTool sharedUtil] requestAFURL:MJ_ME_HistoryExams httpMethod:1 parameters:para succeed:^(id dict) {
        
        NSDictionary *resultDict = [NSDictionary dictionaryWithDictionary:dict];
        NSString *stateCode = [resultDict objectForKey:@"flag"];
        
        if ([stateCode isEqualToString:@"001"])
        {
            MJMyHistoryExamsModel *model = [MJMyHistoryExamsModel modelWithDictionary:resultDict];
            requestSuccess (stateCode,model,nil);
        }
        else
        {
            if ([stateCode isEqualToString:@"203"])
            {
                [[NSUserDefaults standardUserDefaults] setBool:NO forKey:IsLogined];
            }
            requestSuccess (stateCode,nil,[resultDict objectForKey:@"content"]);
        }
        
    } failure:^(NSError *err) {
        
        NSLog(@"%@",err);
        requestFailure (err);
    }];
}

@end
